package com.games.portal.rest;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.games.portal.model.Game;
import com.games.portal.model.GamesContainer;
import com.games.portal.model.builder.ListItemBuilder;

/**
 * 
 * @author hirsche
 *
 */
public class GetGamesRestMethod extends AbstractRestMethod<GamesContainer> {

	private Context mContext;

	private static final URI GAME_URI = URI
			.create("http://192.168.1.104:8080/GamesWebService/rest/gameList/get");
	
	public GetGamesRestMethod() {
	}

	@Override
	protected Request buildRequest() {
		return new Request(Method.GET, GAME_URI, null, null);
	}

	@Override
	protected GamesContainer parseResponseBody(String responseBody) throws Exception {
		List<Game> gameList = new ArrayList<Game>();
		JSONObject json = new JSONObject(responseBody);
		JSONArray jsonArray = json.getJSONArray("Game");
		for (int i=0;i<jsonArray.length();i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Game game = ListItemBuilder.create().addName(jsonObject.getString("name"))
												  .addCategoryName(jsonObject.getString("category_name"))
												  .addImageUrl(jsonObject.getString("image_url"))
												  .addUrl(jsonObject.getString("url"))
												  .build();
			gameList.add(game);
		}
		return new GamesContainer(gameList);

	}

	@Override
	protected Context getContext() {
		return mContext;
	}


}
